package com.tg.app.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;

import com.demo.view.PtzView;
import com.tg.app.R;
import com.tg.app.camera.AVIOCTRLDEFs;
import com.tange.base.toolkit.DimenUtil;
import com.tg.appcommon.android.TGLog;
import com.widget.CircleConsoleView;

/**
 * Describe:
 * Created by Rance Rang on 2019-12-21.
 * <p>
 * Copyright (c) Tange.Inc All rights reserved.
 */
public class PtzControlView extends PtzView implements CircleConsoleView.OnCircleConsoleViewTouchListener {
    public static final String TAG = PtzControlView.class.getSimpleName();

    private static final int PRESET_CIRCLE_SIZE_DP = 200;


    private CircleConsoleView circleConsoleView;

    private float[] mTouchPosition;

    private ImageView mImageView;

    public PtzControlView(Context context) {
        super(context);
        initView(context);
    }

    public PtzControlView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }


    public void setImageView(@DrawableRes int resId) {
        mImageView.setImageResource(resId);
    }

    public PtzControlView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_ptz_control, null);
        addView(view);
        this.mImageView = view.findViewById(R.id.iv_ptz_control);

        Log.i("PTZ_VIEW_TAG", "initView: start ...");

        resizeCircle();

        this.circleConsoleView = view.findViewById(R.id.circleConsoleView);
        this.circleConsoleView.setHandleReaction(this);
        View.OnTouchListener listener = new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (!isClickable()) return true;
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE: {
                        mTouchPosition = new float[2];
                        mTouchPosition[0] = event.getX();
                        mTouchPosition[1] = event.getY();
                        circleConsoleView.invalidate();
                        return true;
                    }
                    case MotionEvent.ACTION_UP: {
                        mTouchPosition = null;
                        if (mIsLandscape){
                            mImageView.setImageResource(R.mipmap.ptz_control_bg_land);
                        }else{
                            mImageView.setImageResource(R.mipmap.ic_tange_switch_ptz);


                        }

                        circleConsoleView.invalidate();
                        if (onPtzControlTouchListener != null) {
                            if (isSendPTZCmd) {
                                TGLog.d(PtzControlView.TAG, "onStopCmd ptzCmd");
                                onPtzControlTouchListener.onStopCmd();
                            } else {
                                TGLog.d(PtzControlView.TAG, "onShortCmd1 ptzCmd = " + ptzCmd);
                                onPtzControlTouchListener.onShortCmd(ptzCmd);
                            }
                            isSendPTZCmd = false;
                            TGLog.d(PtzControlView.TAG, "isSendPTZCmd === false");
                            ptzCmd = -1;
                        }

                        return true;
                    }
                }
                return false;
            }
        };
        circleConsoleView.setOnTouchListener(listener);
        circleConsoleView.setOnLongClickListener(view1 -> {
            TGLog.d(PtzControlView.TAG, "circleConsoleView.setOnLongClickListener onShortCmd");
            onPtzControlTouchListener.onShortCmd(ptzCmd);
            return false;
        });
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);

        // 每次变为可见的时候，重新调整大小
        if (VISIBLE == visibility) {
            resizeCircle();
        }
    }

    /**
     * 圆形图片根据高度，重新调整成正方形 （否则小屏幕会被压缩为长方形导致四个箭头对不准）
     */
    private void resizeCircle() {
        Log.i("PTZ_VIEW_TAG", "resizeCircle ... ");

        if (mImageView == null) {
            Log.i("PTZ_VIEW_TAG", "resizeCircle view is null");
            return;
        }

        mImageView.post(() -> {

            final int parentWidth = getMeasuredWidth();
            final int parentHeight = getMeasuredHeight();
            final int presetCircleSize = DimenUtil.dp2px(getContext(), PRESET_CIRCLE_SIZE_DP);
            final boolean shouldUsePreset = parentHeight > presetCircleSize && parentWidth > presetCircleSize;

            Log.i("PTZ_VIEW_TAG", "resizeCircle ...presetCircleSize  " + presetCircleSize);

            Log.i("PTZ_VIEW_TAG", "resizeCircle ...parent width " + parentWidth);
            Log.i("PTZ_VIEW_TAG", "resizeCircle ...parent height  " + parentHeight);

            Log.i("PTZ_VIEW_TAG", "resizeCircle ...shouldUsePreset  " + shouldUsePreset);

            final int height = mImageView.getMeasuredHeight();
            if (height <= 0) {
                Log.i("PTZ_VIEW_TAG", "resizeCircle:height = 0 , skip ...");
                return;
            }

            Log.i("PTZ_VIEW_TAG", "resizeCircle: height = " + height);

            LayoutParams lp = (LayoutParams) mImageView.getLayoutParams();
            lp.width = shouldUsePreset ? presetCircleSize : height;
            lp.height = shouldUsePreset ? presetCircleSize : height;

            Log.i("PTZ_VIEW_TAG", "resizeCircle ...lp.width  " + lp.width);
            Log.i("PTZ_VIEW_TAG", "resizeCircle ...lp.height  " + lp.height);

            mImageView.setLayoutParams(lp);

            // 按比例调整 四个箭头距离边缘的间距，不能恒定30
            int margin = (int) (lp.width * (30 * 1.0 / 200));

            Log.i("PTZ_VIEW_TAG", "resizeCircle: margin = " + margin);



        });
    }

    @Override
    public float[] getTouchPosition() {
        return this.mTouchPosition;
    }

    @Override
    public void onDirectionChanged(int direction) {
        if (onPtzControlTouchListener == null) return;

        int cmd = 0;
        switch (direction) {
            case CircleConsoleView.AVIOCTRL_PTZ_LEFT:
                cmd = AVIOCTRLDEFs.AVIOCTRL_PTZ_LEFT;

                if (mIsLandscape){
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land);
                    }else{
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land_left);
                    }
                }else{
                    if (mTouchPosition == null){

                        mImageView.setImageResource(R.mipmap.ic_tange_switch_ptz);


                    }else{
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_left);


                    }

                }

                break;
            case CircleConsoleView.AVIOCTRL_PTZ_RIGHT:
                cmd = AVIOCTRLDEFs.AVIOCTRL_PTZ_RIGHT;

                if (mIsLandscape){
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land);
                    }else{
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land_right);
                    }
                }else{
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_ptz);


                    }else{
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_right);


                    }

                }

                break;
            case CircleConsoleView.AVIOCTRL_PTZ_TOP:
                cmd = AVIOCTRLDEFs.AVIOCTRL_PTZ_UP;
                if (mIsLandscape){
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land);
                    }else{
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land_top);
                    }
                }else{
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_ptz);


                    }else{
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_up);

                    }

                }


                break;
            case CircleConsoleView.AVIOCTRL_PTZ_BOTTOM:
                cmd = AVIOCTRLDEFs.AVIOCTRL_PTZ_DOWN;
                if (mIsLandscape){
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land);
                    }else{
                        mImageView.setImageResource(R.mipmap.ptz_control_bg_land_bottom);
                    }
                }else{
                    if (mTouchPosition == null){
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_ptz);


                    }else{
                        mImageView.setImageResource(R.mipmap.ic_tange_switch_down);


                    }
                }

                break;
        }
        TGLog.d(TAG, "cmd = " + cmd);
        if (ptzCmd == -1) {
            ptzCmd = cmd;
            TGLog.d(PtzControlView.TAG, "onLongCmd cmd =" + cmd);
            onPtzControlTouchListener.onLongCmd(ptzCmd);
        } else if (cmd != ptzCmd) {
            ptzCmd = cmd;
            TGLog.d(PtzControlView.TAG, "onLongCmd2 cmd =" + cmd);
            onPtzControlTouchListener.onStopCmd();
            onPtzControlTouchListener.onLongCmd(ptzCmd);
        }
    }
}
